//
//  PhotoSearchResultViewController.m
//  GolfFriend
//
//  Created by Vescky on 14-3-12.
//  Copyright (c) 2014年 vescky.org. All rights reserved.
//

#import "PhotoSearchResultViewController.h"
#import "AppsEngine.h"
#import "SVProgressHUD.h"
#import "photo12Cell.h"
#import "ShareManager.h"

@interface PhotoSearchResultViewController ()<UITableViewDataSource,UITableViewDelegate,photo12CellDelegate,UMSocialUIDelegate> {
    NSMutableArray *resultList;
}

@end

@implementation PhotoSearchResultViewController

@synthesize keyword;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitle:@"下载十二连拍"];
    [super customBackButton];
    [self initView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Private
- (void)initView {
    if (!keyword || keyword.length < 1) {
        [self showNoDataView];
        return;
    }
    [self getDataFromServer];
}

- (void)getDataFromServer {
    [self enableAnimating:YES];
    NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithObjectsAndKeys:keyword,@"name", nil];
    [[AppsEngine shareAppsEngine] submitRequest:@"gfGet12Photo" param:params onCompletion:^(id jsonResponse) {
        [self enableAnimating:NO];
        id contentObj = [jsonResponse objectForKey:@"content"];
        if (contentObj && [contentObj isKindOfClass:NSClassFromString(@"NSArray")]) {
            resultList = [[NSMutableArray alloc] initWithArray:[jsonResponse objectForKey:@"content"]];
        }
        
        if ([resultList count] <= 0) {
            //无结果
            [self showNoDataView];
        }
        else {
            [tbResults setHidden:NO];
            [tbResults reloadData];
        }
    } onError:^(NSError *error) {
        [self enableAnimating:NO];
        [self showNoDataView];
    } method:@"GET"];
}

- (void)enableAnimating:(bool)_enable {
    if (_enable) {
        [viewLoading setFrame:CGRectMake(0, 50, 320, 100)];
        [self.view addSubview:viewLoading];
        [clActivityIndicator startAnimating];
    }
    else {
        [viewLoading removeFromSuperview];
        [clActivityIndicator stopAnimating];
    }
}

- (void)showNoDataView {
    [viewNoData setFrame:CGRectMake(0, 50, 320, 100)];
    [self.view addSubview:viewNoData];
}


#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [resultList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"photo12Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if(!cell){
        cell = [[[NSBundle mainBundle] loadNibNamed:@"photo12Cell" owner:self options:nil] objectAtIndex:0];
    }
    
    if([cell respondsToSelector:@selector(setDataInfo:)]){
        [cell performSelector:@selector(setDataInfo:) withObject:[resultList objectAtIndex:indexPath.row]];
    }
    if([cell respondsToSelector:@selector(setDelegate:)]){
        [cell performSelector:@selector(setDelegate:) withObject:self];
    }
    
    cell.tag = indexPath.row;
    
    [cell setTag:indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 240;
}

- (void)photo12CellButtonDidPress:(id)sender imageContent:(UIImage*)_image {
    
    UIButton *btn = (UIButton*)sender;
    if (btn.tag == 101) {
        //下载
        NSLog(@"下载");
        UIImageWriteToSavedPhotosAlbum(_image, nil, nil, nil);
        [SVProgressHUD showSuccessWithStatus:@"图片已存到相册!"];
    }
    else if (btn.tag == 102) {
        //分享
        NSLog(@"分享");
        [[ShareManager shareManeger] shareToSocialPlatformWithUrl:@"http://wap.baidu.com" title:@"名媛荟分享" text:@"分享我的一张图片!" image:_image parrentViewController:self];
    }
    
}

@end
